﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.View;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.View
{
    public partial class View_PickUpMaterialDetailBusiness : BaseBusiness<View_PickUpMaterialDetail>, IView_PickUpMaterialDetailBusiness, ITransientDependency
    {
        public View_PickUpMaterialDetailBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<View_PickUpMaterialDetail>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<View_PickUpMaterialDetail>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<View_PickUpMaterialDetail, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }
        //朱柏清 注 备货单新建添加显示列表 2022-03-011
        //public async Task<PageResult<View_PickUpMaterialDetail>> GetInventoryDataListAsync(PageInput<QueryGetInventoryDTO> input)
        //{
        //    var q = GetIQueryable();
        //    var where = LinqHelper.True<View_PickUpMaterialDetail>();
        //    var search = input.Search;

        //    //筛选
        //    if (!string.IsNullOrEmpty(input.Search.AreaId))
        //    {
        //        q = q.Where(s => s.StorId.Contains(search.AreaId));
        //    }
        //    if (!string.IsNullOrEmpty(input.Search.MaterialNameOrCode))
        //    {
        //        q = q.Where(s => s.MaterialName.Contains(search.MaterialNameOrCode) || s.MateriaCode.Contains(search.MaterialNameOrCode));
        //    }
        //    if (!string.IsNullOrEmpty(input.Search.ShippingListId))
        //    {

        //        q = q.Where(s => s.NoticeId.Contains(search.ShippingListId));
        //    }
        //    if (!string.IsNullOrEmpty(input.Search.StockNumber))
        //    {
        //        if (input.Search.StockNumber == "0")
        //        {
        //            q = q.Where(s => s.StockNumber == 0);
        //        }
        //        else if (input.Search.StockNumber == "1")
        //        {
        //            q = q.Where(s => s.StockNumber > 0);
        //        }
        //    }

        //    return await q.Where(where).GetPageResultAsync(input);
        //}
        /// <summary>
        /// 朱柏清 备货单新建添加显示列表 2022-03-011
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<View_PickUpMaterialDetail>> GetInventoryDataListAsync(PageInput<QueryGetInventoryDTO> input)
        {
            var search = input.Search;
            //string sqlstr = @"SELECT * from View_PickUpMaterialDetail WHERE NoticeId='"+ input.Search.ShippingListId + "' ";
            string sqlstr = @"select * from (SELECT a.*,b.Slot FROM (select * from View_PickUpMaterialDetail where NoticeId='" + input.Search.ShippingListId + "' ) as a left join PB_DesignDetail as b on a.PB_DesignListInfo=b.Id )as t where t.StockNumber>0 ";

            //var q = GetIQueryable();
            //var where = LinqHelper.True<View_PickUpMaterialDetail>();

            //筛选
            if (!string.IsNullOrEmpty(input.Search.AreaId))
            {
                sqlstr += " and StorId like '"+ search.AreaId + "' ";
            }
            if (!string.IsNullOrEmpty(input.Search.MaterialNameOrCode))
            {

                sqlstr += " and (MaterialName like '%" + search.MaterialNameOrCode + "%' or MaterialCode like '%" + search.MaterialNameOrCode + "%') ";
            }
            if (!string.IsNullOrEmpty(input.Search.StockNumber))
            {

                if (input.Search.StockNumber == "0")
                {
                    sqlstr += " and StockNumber =0 ";
                }
                else if (input.Search.StockNumber == "1")
                {
                    sqlstr += " and StockNumber >0 ";
                }
            }
            sqlstr += @"  ORDER BY Slot ";
            var InventoryData =await Db.GetListBySqlAsync<View_PickUpMaterialDetail>(sqlstr);
            int num = InventoryData.Count;
            var list = InventoryData
               .Skip((input.PageIndex - 1) * input.PageRows)
               .Take(input.PageRows).ToList();
               //.ToListAsync();

            return new PageResult<View_PickUpMaterialDetail> { Data = list, Total = num };
           // return await q.Where(where).GetPageResultAsync(input);
        }
        public async Task<View_PickUpMaterialDetail> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(View_PickUpMaterialDetail data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(View_PickUpMaterialDetail data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}